package com.supertimetable.activities;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.supertimetable.bean.ScheduleTable;
import com.supertimetable.fragment.ClassFragment;
import com.supertimetable.fragment.MoreFragment;
import com.supertimetable.fragment.StudentFragment;
import com.supertimetable.fragment.Teacher_Fragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    /***********
     * 数据实例化
     */
    /*******************

    组件实例化

    */

    private TextView checkOut;
    private Button resist;
    private TextView[][] course;
    private EditText verificationCode;
    private ImageView verificationImage;
    private TextView teacherSchedule,studentSchedule,classSchedule,more;
    private AutoCompleteTextView name = null;
    private ArrayAdapter<String> arrayAdapter;
    private String[] arr={"acaa","abbb","acdcc"};
    private List<CharSequence> date = null;
    private List<CharSequence> teacherNames = null;
    private ArrayAdapter<CharSequence> eduAdaptor = null;
    private ArrayAdapter<CharSequence> eduAdaptor1 = null;
    private Spinner eduSpinner = null;
    private Spinner eduSpinner1 = null;

    //各个Fragmentdingyi
    private StudentFragment student;
    private Teacher_Fragment teacher;
    private ClassFragment class1;
    private MoreFragment more1;
    private ScheduleTable schedule;

    //4个linearlayout
    private LinearLayout button1,button2,button3,button4;

    //底部导航四个image和textview
    private ImageView imageview1,imageview2,imageview3,imageview4;
    private TextView textview1,textview2,textview3,textview4;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       /* initViews();
        eventsViews();*/
        initDate();
        /*initTeacherName();*/



        //四个fragment的监听事件

        button1 = (LinearLayout) findViewById(R.id.button1);
        button1.setOnClickListener(this);
        button2 = (LinearLayout) findViewById(R.id.button2);
        button2.setOnClickListener(this);
        button3 = (LinearLayout) findViewById(R.id.button3);
        button3.setOnClickListener(this);
        button4 = (LinearLayout) findViewById(R.id.button4);
        button4.setOnClickListener(this);

        //底部导航四个image和textview实例化
        imageview1 = (ImageView) findViewById(R.id.imageview1);
        imageview2 = (ImageView) findViewById(R.id.imageview2);
        imageview3 = (ImageView) findViewById(R.id.imageview3);
        imageview4 = (ImageView) findViewById(R.id.imageview4);
        textview1 = (TextView) findViewById(R.id.textview1);
        textview2 = (TextView) findViewById(R.id.textview2);
        textview3 = (TextView) findViewById(R.id.textview3);
        textview4 = (TextView) findViewById(R.id.textview4);


       /* //点击进入教室课表查询页面
        teacherSchedule = (TextView) findViewById(R.id.teacherSchedule);
        teacherSchedule.setOnClickListener(this);

        //点击进入班级课表查询
        classSchedule = (TextView) findViewById(R.id.classSchedule);
        classSchedule.setOnClickListener(this);

        //点击进入学生课表查询
        studentSchedule = (TextView) findViewById(R.id.studentSchedule);
        studentSchedule.setOnClickListener(this);

        //点击进入更多页面
        more = (TextView) findViewById(R.id.more);
        more.setOnClickListener(this);
*/
        /*verificationImage = (ImageView) findViewById(R.id.verificationImage);
        verificationImage.setImageDrawable(getResources().getDrawable(R.drawable.verimg));
        verificationCode = (EditText) findViewById(R.id.verificationCode);
        resist = (Button) findViewById(R.id.resist);
        resist.setOnClickListener(this);
*/
        //点击获取查询的课表
        checkOut = (TextView) findViewById(R.id.checkOut);
        checkOut.setOnClickListener(this);

    }

    /*private void initTeacherName() {
        eduSpinner1 = (Spinner) findViewById(R.id.spiner02);
        eduSpinner1.setPrompt("选择教师");
        teacherNames = new ArrayList<CharSequence>();
        teacherNames.add("张三");
        teacherNames.add("李四");
        teacherNames.add("王五");
        teacherNames.add("赵六");
        eduAdaptor1 = new ArrayAdapter<CharSequence>(this,android.R.layout.simple_list_item_1,teacherNames);
        eduAdaptor1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        eduSpinner1.setAdapter(eduAdaptor1);
    }*/

    private void initDate() {
        eduSpinner = (Spinner) findViewById(R.id.spiner01);
        eduSpinner.setPrompt("选择日期");
        date = new ArrayList<CharSequence>();
        date.add("2016年第一学期");
        date.add("2016年第二学期");
        date.add("2017年第一学期");
        date.add("2017年第二学期");
        eduAdaptor = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_list_item_1, date);
        eduAdaptor.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        eduSpinner.setAdapter(eduAdaptor);







    }






    /*private void eventsViews() {
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arr);
        name.setAdapter(adapter);
    }

    private void initViews() {
         name = (AutoCompleteTextView) findViewById(R.id.name);
    }*/

        private void replaceFragment(Fragment fragment) {
        FragmentManager fragemntManger = getSupportFragmentManager();
        FragmentTransaction traction  = fragemntManger.beginTransaction();
        traction.replace(R.id.scheduleTable,fragment);
        traction.commit();
        }
        private void replaceFragment(int index) {
       // 由于是引用了V4包下的Fragment，所以这里的管理器要用getSupportFragmentManager获取
        FragmentManager fragmentManager = getSupportFragmentManager();
       // 开启事务
        FragmentTransaction transaction = fragmentManager.beginTransaction();
       // 隐藏所有Fragment
        hideFragment(transaction);
        switch (index) {
           case 0:
               if (student == null) {
                   student = new StudentFragment();
                   transaction.add(R.id.head, student);
               } else {
                   transaction.show(student);
               }
               break;
           case 1:
               if (class1 == null) {
                   class1 = new ClassFragment();
                   transaction.add(R.id.head, class1);
               } else {
                   transaction.show(class1);
               }
               break;
           case 2:
               if (teacher == null) {
                   teacher = new Teacher_Fragment();
                   transaction.add(R.id.head, teacher);
               } else {
                   transaction.show(teacher);
               }
               break;
           case 3:
               if (more1 == null) {
                   more1 = new MoreFragment();
                   transaction.add(R.id.head, more1);
               } else {
                   transaction.show(more1);
               }

               break;
           case 4:
               if (schedule == null) {
                   schedule = new ScheduleTable();
                   transaction.add(R.id.scheduleTable, schedule);
               } else {
                   transaction.show(schedule);
               }

               break;
           default:
               break;
       }
       // 提交事务
       transaction.commit();
   }
    //隐藏Fragment
    private void hideFragment(FragmentTransaction transaction) {
        if (teacher != null) {
            transaction.hide(teacher);
        }
        if (student != null) {
            transaction.hide(student);
        }
        if (more1 != null) {
            transaction.hide(more1);
        }
        if (class1 != null) {
            transaction.hide(class1);
        }

    }

    @Override
    public void onClick(View v) {
        // 在每次点击后将所有的底部按钮(ImageView,TextView)颜色改为灰色，然后根据点击着色
        restartBotton();
        /*if(verificationCode.getText().toString().equals("1234")){
            switch (v.getId()) {
                case R.id.studentSchedule:

                    replaceFragment(0);
                    break;
                case R.id.classSchedule:

                    replaceFragment(1);
                    break;
                case R.id.teacherSchedule:
                    replaceFragment(2);
                    break;
                case R.id.more:
                    replaceFragment(3);
                    break;
                case R.id.checkOut:
                    replaceFragment(4);
                    break;
                default:
                    break;
            }
        }*/

        switch (v.getId()) {
            case R.id.button3:
                imageview3.setImageResource(R.drawable.student2);
                textview3.setTextColor(Color.rgb(18,150,219));
                replaceFragment(0);
                break;
            case R.id.button2:
                imageview2.setImageResource(R.drawable.class2);
                textview2.setTextColor(Color.rgb(18,150,219));
                replaceFragment(1);
                break;
            case R.id.button1:
                imageview1.setImageResource(R.drawable.teacher2);
                textview1.setTextColor(Color.rgb(18,150,219));
                replaceFragment(2);
                break;
            case R.id.button4:
                imageview4.setImageResource(R.drawable.more2);
                textview4.setTextColor(Color.rgb(18,150,219));
                replaceFragment(3);
                break;
            case R.id.checkOut:
                if(teacher!=null || class1!=null || student!=null){
                    replaceFragment(4);
                }

                break;
            default:
                break;
        }

    }

    private void restartBotton() {
        // ImageView置为灰色
        imageview1.setImageResource(R.drawable.teacher1);
        imageview2.setImageResource(R.drawable.class1);
        imageview3.setImageResource(R.drawable.student1);
        imageview4.setImageResource(R.drawable.more1);
        // TextView置为白色
        textview1.setTextColor(Color.rgb(112,112,112));
        textview2.setTextColor(Color.rgb(112,112,112));
        textview3.setTextColor(Color.rgb(112,112,112));
        textview4.setTextColor(Color.rgb(112,112,112));
    }

   /* public  void resist(){
        resist = (Button) findViewById(R.id.resist);
        resist.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                switch(v.getId()){
                    case R.id.resist:
                        replaceFragment(new CheckOutActivity());
                        break;
                    default:break;
                }
            }
        });
    }*/


}
